module vfs.jsonwriter;

import utils.log;
import std.stream;
public import std.json;
import exceptions;
import std.string;
import std.file;
import core.thread;

// TODO: should format before write
class JSONWriter {
    private string myFileName;

    public this(in string fileName) {
        myFileName = fileName;
    }

    public string getFileName() const shared {
        return myFileName;
    }

    public void write(JSONValue value) shared {
        OutputStream outputStream = new BufferedFile(myFileName, FileMode.OutNew);
        (new Thread({
            outputStream.writeLine(toJSON(&value));            
            scope(exit) {
                outputStream.close();
            }
        })).start();
        scope(failure) {
            outputStream.close();
        }
    }
}

